Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

205
Visualizações
React-Router: Display homepage if not authenticated or dashboard if authenticated in at "/"

I have stored in redux if a user is authenticated or not. What I want to do is, once I go to localhost:3000/ I want it to display homepage if user is not authenticated or display the dashboard if the user is authenticated. This is what I have so far.

export default (
 <Route path="/" component={App} >
   <IndexRoute component={HomePage} />
   <Route path="/dashboard" component={Dashboard} />
   <Route path="*" component={NotFound} />
</Route>
);

I would like the IndexRoute to be conditional on authentication.

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

In the HomePage component you can render two other components based on authentication. For example

class HomePage extends Component {
  //...
  render() {
    if (currentUser) return <Dashboard /> // if user exists in the store
    else if (fetchingCurrentUser) return <LoadingView /> // if you need to fetch the user show a loading page
    else return <UnauthenticatedHomeView /> // else, return a homepage for unauthenticated users
  }
}

This way you can eliminate the '/dashboard' route altogether so the user doesn't have to travel to that route to get to their dashboard. Also, HomePage just acts as a container for the different possible components it can render inside of it.

Your routes would then look like this.

export default (
 <Route path="/" component={App} >
   <IndexRoute component={HomePage} />
   <Route path="*" component={NotFound} />
</Route>
);

Another note: you can also hook up the onEnter function to a route to run when the route is about to entered. Here you can do some other authenticated to protect a route. However, the example implementation above would have to change if you use onEnter but you now know you have that option.

over 4 years ago · Santiago Trujillo Relatório

0

You could use conditional rendering in JSX.

export default (
 <Route path="/" component={App} >
    {isLoggedIn ? (
      <IndexRoute component={HomePage} />
    ) : (
      <IndexRoute component={Dashboard} />
    )}
</Route>
);
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda